package com.blog.controller;


import com.blog.entity.SysPlace;
import com.blog.mapper.SysPlaceMapper;
import com.blog.redis.RedisUtil;
import com.blog.service.Impl.SysPlaceServiceImpl;

import com.blog.util.dto.AjaxResult;
import com.blog.util.pinyin.PinYinUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.util.List;

/**
 * @author cgx
 * @Date 2021/9/1 20:49
 *
 * 住址控制类
 */
@Controller
@Slf4j
public class SysPlaceController {

    @Resource
    SysPlaceMapper placeMapper;

    @Resource
    SysPlaceServiceImpl placeService;

    @Resource
    RedisUtil redisUtil;

    /**
     * 通过id获取该地点的全称
     * @param placeId 地点id
     * @return 地点数据
     */
    @PostMapping("/place/getPlaceNameById")
    @ResponseBody
    public AjaxResult getPlaceNameById(@PathParam("placeId") String placeId){
        SysPlace place = placeService.getById(placeId);
        return AjaxResult.success(place);
    }

    /**
     * 获取地址，地址树展示
     * @return 地点数据
     */
    @GetMapping("/place/getPlaceForTree")
    @ResponseBody
    public AjaxResult getPlaceForTree(){
        List<SysPlace> sysPlaces = placeMapper.selectAllForTree();
        return AjaxResult.success(sysPlaces);
    }

    /**
     * 根据id查询地点信息
     * @param
     * @return
     */
    @PostMapping("/place/getPlaceById")
    @ResponseBody
    public AjaxResult getPlaceById(@PathParam("placeId") String placeId){
        SysPlace byId = placeService.getById(placeId);
        if (byId == null){
            return AjaxResult.error("没有查询到相应的信息");
        }
         return AjaxResult.success(byId);
    }

    /**
     * 增加地点
     * @param
     * @return
     */
    @PostMapping("/system/place/savePlace")
    @ResponseBody
    public AjaxResult placeAdd(@PathParam("placePid") String placePid, @PathParam("placeText") String placeText, @PathParam("placeName") String placeName, @PathParam("placeLongitude") String placeLongitude, @PathParam("placeDimension") String placeDimension, @PathParam("placeType") String placeType, @PathParam("parentPlaceType") String parentPlaceType){
        if (placePid != null){
            if (placePid.isEmpty()){
                return AjaxResult.error("地点为空");
            }
        }
        String id = "";
        if (placeText != null){
            if (!placeText.isEmpty()){
                id = PinYinUtil.getPinyin(placeText);
            }else {
                return AjaxResult.error("地点为空");
            }
        }
        SysPlace byId = placeService.getById(id);
        if (byId!=null){
            return AjaxResult.error("地点已存在");
        }
        if (placeName != null){
            if (placeName.isEmpty()){
                return AjaxResult.error("地点为空");
            }
        }
        if (placeType != null){
            if (placeType.isEmpty()){
                return AjaxResult.error("地点为空");
            }
        }
        SysPlace sysPlace = new SysPlace();
        sysPlace.setId(id);
        sysPlace.setPid(placePid);
        sysPlace.setText(placeText);
        sysPlace.setPlace(placeName);
        sysPlace.setLongitude(placeLongitude);
        sysPlace.setDimension(placeDimension);
        sysPlace.setActive(1);
        if ("true".equals(parentPlaceType)){
            placeType = "3";
        }
        sysPlace.setType(placeType);
        boolean save = placeService.save(sysPlace);
        if (save){
            return AjaxResult.success();
        }else {
            return AjaxResult.error("保存失败");
        }
    }

    /**
     * 根据id删除地点信息
     * @param
     * @return
     */
    @PostMapping("/system/place/deleteById")
    @ResponseBody
    public AjaxResult delPlaceById(@PathParam("placeId") String placeId){
        if (!placeId.isEmpty()) {
            AjaxResult ajaxResult = placeService.delPlaceById(placeId);
            if (ajaxResult.getState()){
                return AjaxResult.success();
            }else {
                return ajaxResult;
            }
        } else {
            return AjaxResult.error( "id为空");
        }
    }
}
